package com.es.designmode.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * 动态代理
 *
 * @author Will
 */
public class ClientSide {

    public static void main(String[] args) {
        sayByCode(true);

        System.out.println("*************");

        sayByCode(false);
    }

    private static void sayByCode(boolean b) {
        if (b) {
            ISay hello = new SayHello();
            dynamicProxy(hello);
        } else {
            ISay bye = new SayBye();
            dynamicProxy(bye);
        }
    }

    /**
     * 动态代理基本方法
     *
     * @param obj
     */
    private static void dynamicProxy(Object obj) {
        InvocationHandler invocationHandler = new ProxyHandler(obj);

        ISay s = (ISay) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), invocationHandler);
        s.say();
    }
}
